package com.team16.budgetme;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;


import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class EditAccountActivity extends SherlockListActivity{
 	private DataHandler data;
    private ArrayList<Account> accounts;
    private Bundle extras;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = new DataHandler(this);
        accounts = data.getAllAccounts();
        extras = getIntent().getExtras();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        ArrayAdapter<Account> adapter = new AccountAdapter(this, R.layout.account_list_row,
        		accounts);
        setContentView(R.layout.activity_editaccount);
        setListAdapter(adapter);
        
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            	AlertDialog dialog = changeAccountNameDialog(arg1, arg2);
                dialog.show();
            }
        });
        
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
             
                AlertDialog dialog = deleteAccountDialog(arg1, arg2);
                dialog.show();
                return false;
            }
        });
       
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case android.R.id.home:
                intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private AlertDialog changeAccountNameDialog(View view, final int position){
    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);
         LayoutInflater inflater = getLayoutInflater();
         View layout = inflater.inflate(R.layout.name_account_dialog, null);
         final EditText name = (EditText) layout.findViewById(R.id.account_name);
         
         builder.setTitle(R.string.name_account)
                 .setView(layout)
                 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                    	 String accountName = name.getText().toString();
                    	 
                    	 if (accountName.matches("")) {
                             return;
                         }
                         changeAccountName(position,accountName);                       
                     }
                 })
                 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.cancel();
                     }
                 });
         
         return builder.create();
    }
    
    private AlertDialog deleteAccountDialog(View view, final int position){
   	 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.delete_account_dialog, null);
        
        builder.setTitle(R.string.delete_account)
                .setView(layout)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteAccount(position);                       
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        
        return builder.create();
   }
    
    private void deleteAccount(int index){
    	Account a = accounts.get(index);
    	data.deleteAccount(a);
    	finish();
        startActivity(getIntent());
       
    }
    
    private void changeAccountName(int index,String newName){
    	Account a = accounts.get(index);
    	data.changeAccountName(a, newName);
    	finish();
    	startActivity(getIntent());
    }
    private class AccountAdapter extends ArrayAdapter<Account> {
    	Context context;
    	ArrayList<Account> accounts;
    	
    	public AccountAdapter(Context context, int resource, ArrayList<Account> accounts){
    		super(context, resource, accounts);
    		this.context = context;
    		this.accounts = accounts;
    	}
    	public View getView(int position, View convertView, ViewGroup parent){
    		LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		View layout = inflator.inflate(R.layout.account_list_row, parent, false);
    		Account a = accounts.get(position);
    		TextView name = (TextView) layout.findViewById(R.id.account_name);
    		TextView balance = (TextView) layout.findViewById(R.id.account_total);
    		String balanceStr = NumberFormat.getCurrencyInstance().format(a.getBalance());
    		
			if (a.getBalance().compareTo(new BigDecimal(0.0)) < 0) {
		           balance.setTextColor(Color.RED);
		       }
    		name.setText(a.getName());
    		balance.setText(balanceStr);
    		
    		return layout;
    	}
    }

}
